/**
 * Created by tanbingqin on 2019/09/12.
 */
import {messages} from "share/common";
import React from 'react';
import { connect } from 'react-redux';
import { Divider } from 'antd';

class TravelPrompt extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showValue: '' //当前展示的提示语
    }
  }

  componentDidMount() {
    this.initPromptShowValue();
  }

  componentWillReceiveProps(nextProps) {
    this.initPromptShowValue(nextProps);
  }

  initPromptShowValue = (nextProps) => {
    const { promptList, type } = nextProps || this.props;
    let showValue = '';
    promptList && promptList.map(item => {
      if (item.type === type && item.enable) {
        showValue = this.processLanguage(item.tipsI18n);
      }
    });
    this.setState({showValue});
  };

  processLanguage = (i18n) => {
    const { language } = this.props;
    let result = '';
    i18n && i18n.length && i18n.map(oneLanguage => {
      if (language.code && oneLanguage.language && language.code.toLowerCase() === oneLanguage.language.toLowerCase()) {
        result = oneLanguage.value;
      }
    });
    return result;
  };

  render() {
    const { showValue } = this.state;
    return showValue ? (
      <div>
        <div style={{color: '#6C788C', lineHeight: '20px'}}>
          {showValue}
        </div>
        <Divider style={{margin: '12px 0'}} />
      </div>
    ) : <div />
  }
}

function mapStateToProps(state) {
  return {
    language: state.main.language
  }
}

TravelPrompt.propTypes = {
  promptList: React.PropTypes.array, //行程提示配置列表
  type: React.PropTypes.number //要展示的行程类型
};

TravelPrompt.defaultProps = {
  promptList: []
};

export default connect(mapStateToProps)(TravelPrompt);
